package com.um.pasantiaum.truck;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.um.pasantiaum.R;

public class WhereIsMyTruckActivity extends FragmentActivity {

	public final static String CLICKED_TRUCK = "CLICKED_TRUCK";

	private ParseObject truckEmpresa;
	private ParseUser mTruck;

	private GoogleMap mMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_map);

		ParseQuery<ParseUser> userQuery = ParseUser.getQuery();

		userQuery.getInBackground(getIntent().getStringExtra(CLICKED_TRUCK),
				new GetCallback<ParseUser>() {

					@Override
					public void done(ParseUser object1, ParseException e) {
						if (e == null) {
							mTruck = object1;
							
							doMapStuff();
							
						} else {
							Toast.makeText(WhereIsMyTruckActivity.this,
									e.getMessage(), Toast.LENGTH_LONG).show();
						}
					}
				});

		final Handler handler = new Handler();
		Timer timer = new Timer();
		TimerTask doAsynchronousTask = new TimerTask() {
			@Override
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						if (truckEmpresa == null || truckEmpresa.getParseGeoPoint("position") == null)
							return;							
						try {
							doMapStuff();
						} catch (Exception e) {
							// TODO Auto-generated catch block
						}
					}
				});
			}
		};
		timer.schedule(doAsynchronousTask, 0, 1 * 5 * 1000);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setUpMapIfNeeded();
		// setUpLocationClientIfNeeded();
		// mLocationClient.connect();

	}

	private void setUpMapIfNeeded() {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (mMap == null) {
			// Try to obtain the map from the SupportMapFragment.
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();
			// Check if we were successful in obtaining the map.
			if (mMap != null) {

				// mMap.setOnMyLocationButtonClickListener(this);
			}
		}
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// getSupportMenuInflater().inflate(R.menu.options_menu, menu);
	//
	// // refresh menu item
	// refresh = menu.findItem(R.id.refresh_option_item);
	//
	// return super.onCreateOptionsMenu(menu);
	// }

	private void doMapStuff() {

		if (mTruck == null)
			return;

		ParseQuery<ParseObject> pq = ParseQuery.getQuery("Truck");

		pq.whereEqualTo("truck", mTruck);

		try {
			truckEmpresa = pq.getFirst();

			if (truckEmpresa.getParseGeoPoint("position") == null) {
				// ACA PONER EL CODIGO DE QUE SALTA EL TEXTO

				Toast.makeText(WhereIsMyTruckActivity.this,
						"No hay datos de la poscion de este camion",
						Toast.LENGTH_LONG).show();
				return;
			}
			
			MarkerOptions opciones = new MarkerOptions();

			opciones.position(new LatLng(
					truckEmpresa.getParseGeoPoint("position")
					.getLatitude(), truckEmpresa
					.getParseGeoPoint("position")
					.getLongitude()));
			
			opciones.title(mTruck.getString("nombre"));
			opciones.snippet(truckEmpresa.getUpdatedAt().toString());
			
			opciones.icon(BitmapDescriptorFactory.fromResource(R.drawable.truckicon));
			
			mMap.clear();
			
			mMap.addMarker(opciones).showInfoWindow();

			mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
					truckEmpresa.getParseGeoPoint("position").getLatitude(),
					truckEmpresa.getParseGeoPoint("position").getLongitude()),
					14.0f));
		} catch (ParseException e1) {
			Toast.makeText(WhereIsMyTruckActivity.this, e1.getMessage(),
					Toast.LENGTH_LONG).show();
			return;
		}

	}
}
